package flatlandRPG;

import java.util.Stack;
import java.awt.geom.Rectangle2D;
import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.input.Keyboard;

/**
 * The user-controlled player.
 *
 * @author evant
 * @author trev
 * @version April 15, 2011
 */
public class Player
    extends GLRectangle
    implements ILogic, IMoveable
{
    /**
     * The speed of the player.
     */
    public static final float SPEED = 0.01f;

    private Stack<Vector2f>   lastLocations;


    /**
     * Create a new Player object.
     */
    public Player()
    {
        super(0.1f, 0.1f);
        setDrawStyle(LINE);
        lastLocations = new Stack<Vector2f>();
    }


    // ----------------------------------------------------------
    /**
     * Moves the player by the given vector.
     *
     * @param direction
     *            the direction vector to move the player
     */
    public void move(Vector2f direction)
    {
        lastLocations.push(new Vector2f(getX(), getY()));
        Vector2f.add(getLocation(), direction, getLocation());
    }


    /**
     * Moves the player in response to the arrow keys.
     */
    @Override
    public void doLogic()
    {
        if (TestableKeyboard.isKeyDown(Keyboard.KEY_UP))
        {
            move(new Vector2f(0, SPEED));
        }

        if (TestableKeyboard.isKeyDown(Keyboard.KEY_DOWN))
        {
            move(new Vector2f(0, -SPEED));
        }

        if (TestableKeyboard.isKeyDown(Keyboard.KEY_LEFT))
        {
            move(new Vector2f(-SPEED, 0));
        }

        if (TestableKeyboard.isKeyDown(Keyboard.KEY_RIGHT))
        {
            move(new Vector2f(SPEED, 0));
        }
    }


    @Override
    public void collision(ISolid solid)
    {
        while (solid.collidedWith(this))
        {
            if (!lastLocations.isEmpty())
            {
                setLocation(lastLocations.pop());
            }
            else
            {
                break;
            }
        }
        lastLocations.clear();
    }


    @Override
    public Rectangle2D getBounds()
    {

        return new Rectangle2D.Float(getX() - getWidth() / 2, getY()
            - getHeight() / 2, getWidth(), getHeight());
    }


    /**
     * Sets the Entity's location to a new location
     *
     * @param location
     *            A new location
     */
    @Override
    public void setLocation(Vector2f location)
    {
        super.setLocation(location);
    }
}
